//
//  UserViewController.m
//  练习
//
//  Created by hcios on 2017/7/5.
//  Copyright © 2017年 鹿林. All rights reserved.
//


#import "UserViewController.h"
#import "UserView.h"
#import "UserLikesViewController.h"
#import "UserYJViewController.h"
#import "SelectedView.h"
#import "YJModel.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "UserModel.h"


#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface UserViewController ()<UserViewDelegate>{
    
    NSString *nameString;
    NSString *imageString;
    NSString *genderString;
    NSString *trip_countNum;
    
    NSMutableArray *dataRowArray;
    
    UserView *views;
    
    NSMutableArray *gzArray;
}

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //网络请求
    [self netWorking];
    self.title = [NSString stringWithFormat:@"%@",_userName];
    
  
}

-(void)setupViews{
    
    //头部view
    views = [[UserView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    views.backgroundColor = [UIColor redColor];
    views.delegate = self;
    [views.authorImage sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"头像占位图"]];
    views.nameLabel.text = [NSString stringWithFormat:@"%@",nameString];
    views.countLabel.text = [NSString stringWithFormat:@"%@篇游记",trip_countNum];
    views.backgroundColor = [UIColor colorWithRed:214.0 / 255.0 green:226.0 / 255.0 blue:238.0 / 255.0 alpha:1.0];
    [self.view addSubview:views];
    //滑动VC
    UserYJViewController * YVC = [[UserYJViewController alloc] init];
    UserLikesViewController * LVC = [[UserLikesViewController alloc] init];
    
    YVC.userID = _userID;
    YVC.userImage = imageString;
    LVC.userID = _userID;
    NSArray * VCArr = @[YVC,LVC];
    NSArray * titleArr = @[@"游记",@"喜欢"];
    SelectedView * SMVC = [[SelectedView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(views.frame), SCREEN_WIDTH, SCREEN_HEIGHT) buttonName:titleArr controllers:VCArr parentController:self];
    [self.view addSubview:SMVC];
}


#pragma mark - 网络请求
-(void)netWorking{
    
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/users/%@.json",_userID];
    NSDictionary *param = @{@"page":@(1)};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //获取上方view的user信息
        nameString = responseObject[@"name"];
        imageString = responseObject[@"image"];
        genderString = responseObject[@"gender"];
        trip_countNum = responseObject[@"trips_count"];
        UserModel *model = [[UserModel alloc] initWithGZDict:responseObject];
        [gzArray addObject:model];
        [self setupViews];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
   
}


#pragma mark - delegate
-(void)gzBtn:(id)sender{
    
    UIButton *senderBtn = sender;
    if (senderBtn.selected == NO) {
        [senderBtn setTitle:@"取消关注" forState:UIControlStateNormal];
        [senderBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
        senderBtn.selected = YES;
    }else{
        [senderBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
        [senderBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
        senderBtn.selected = NO;
    }
}

@end
